package com.neuedu.util;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.Controller;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.builder.Service;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.neuedu.core.BaseEntity;

import java.util.HashMap;
import java.util.Map;

public class AutoCoding {
    static final String URL = "jdbc:mysql://10.25.133.45:3306/db?serverTimezone=Asia/Shanghai";
    public static void main(String[] args) {
        // 获取父项目路径
        String parent = System.getProperty("user.dir");
        // 定义子项目
        String module = "student";
        // 定义生成的表名
        String table = "user_answer";
        // 创建生成器
        FastAutoGenerator generator = FastAutoGenerator.create(
                URL,
                "root",
                "hj123456"
        );
        // 全局配置
        generator.globalConfig(builder -> {
            // 配置作者
            builder.author("wangyu")
                   .disableOpenDir();
        });
        // 包配置
        generator.packageConfig(builder -> {
            // 父包统一设置为com.neuedu
            builder.parent("com.neuedu");
            // 设置生成路径
            Map<OutputFile, String> pathInfo = new HashMap<>();
            pathInfo.put(OutputFile.entity, parent + "/entity/src/main/java/com/neuedu/entity");
            pathInfo.put(OutputFile.mapper, parent + "/" + module + "/src/main/java/com/neuedu/mapper");
            pathInfo.put(OutputFile.xml, parent + "/" + module + "/src/main/resources/com/neuedu/mapper");
            pathInfo.put(OutputFile.service, parent + "/" + module + "/src/main/java/com/neuedu/service");
            pathInfo.put(OutputFile.serviceImpl, parent + "/" + module + "/src/main/java/com/neuedu/service/impl");
            pathInfo.put(OutputFile.controller, parent + "/" + module + "/src/main/java/com/neuedu/controller");
            builder.pathInfo(pathInfo);
        });
        // 策略配置
        generator.strategyConfig(builder -> {
            // 生成的表
            builder.addInclude(table);
            // 设置entity策略
            Entity.Builder entityBuilder = builder.entityBuilder();
            // 将表名和列名全部下划线转驼峰
            entityBuilder.naming(NamingStrategy.underline_to_camel)
                    .columnNaming(NamingStrategy.underline_to_camel);
            // 开启Lombok
            entityBuilder.enableLombok();
            // 继承BaseEntity 不生成id
            entityBuilder.superClass(BaseEntity.class);
            entityBuilder.addSuperEntityColumns("id");
            // controller策略
            Controller.Builder controllerBuilder = builder.controllerBuilder();
            // 生成RestController
            controllerBuilder.enableRestStyle();
            // service策略
            Service.Builder serviceBuilder = builder.serviceBuilder();
            serviceBuilder.formatServiceFileName("%sService");
        });
        // 模版引擎
        generator.templateEngine(new FreemarkerTemplateEngine());
        // 生成代码
        generator.execute();
    }
}
